﻿using ShenNius.Blog.Common;
using ShenNius.Blog.ViewModels;
using ShenNius.Blog.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using X.PagedList;
namespace ShenNius.Blog.Controllers
{
    public class HomeController : Controller
    {
        public HttpHelper _httpHelper { get; set; }
        private readonly IOptionsMonitor<DomainConfig> _domainConfig;
        private readonly IOptionsMonitor<SiteConfig> _siteConfig;
        public HomeController(HttpHelper httpHelper, IOptionsMonitor<DomainConfig> domainConfig, IOptionsMonitor<SiteConfig> siteConfig)
        {
            _httpHelper = httpHelper;
            _domainConfig = domainConfig;
            _siteConfig = siteConfig;
        }

        [HttpGet("/")]
        public async Task<IActionResult> Index()
        {
            var model = await _httpHelper.GetAsync<ApiResult<IndexVM>>($"/api/index/get-index?siteId={_siteConfig.CurrentValue.GlobalSiteId}");
            if (model == null)
                return NotFound();
            int link=(int) AdvEnum.FriendlyLink;
            var data = await _httpHelper.GetAsync<ApiResult<List<CmsAdvListVM>>>($"/api/index/get-adv-list?siteId={_siteConfig.CurrentValue.GlobalSiteId}&&type={link}");
            model.Data.FriendlyLinkList = data?.Data;
            return View(model.Data);
        }
        [HttpGet]
        public async Task<IActionResult> Detail(int id, string parentColumnUrl, string childColumnUrl = null)
        {
            bool sign;
            if (string.IsNullOrEmpty(childColumnUrl))
            {
                sign = await IsExistColumnUrl(parentColumnUrl);
            }
            else
            {
                sign = await IsExistColumnUrl(parentColumnUrl, childColumnUrl);
            }
            if (!sign)
                return NotFound();
            string queryString = $"/api/index/get-detail?id={id}&parentColumnSpell={parentColumnUrl}&childColumnspell={childColumnUrl}&siteId={_siteConfig.CurrentValue.GlobalSiteId}";
            var model = await _httpHelper.GetAsync< ApiResult<ArticleDetailVM>>(queryString);
            if (model.Data == null)
                return NotFound();            
            return View(model.Data);
        }

        [HttpGet]
        public async Task<IActionResult> NewsList(string parentColumnUrl = "", string childColumnUrl = "", int page = 1)
        {
            ViewBag.CurrentPage = page;
            string url = string.Empty;string currentColumnUrl = string.Empty;
            bool sign;
            if (string.IsNullOrEmpty(childColumnUrl))
            {
                sign = await IsExistColumnUrl(parentColumnUrl);
            }
            else
            {
                sign = await IsExistColumnUrl(parentColumnUrl, childColumnUrl);
            }
            if (!sign)
                return NotFound();
            string queryString = $"/api/index/get-list?page={page}&siteId={_siteConfig.CurrentValue.GlobalSiteId}";
            if (!string.IsNullOrEmpty(parentColumnUrl))
            {
                queryString += $"&parentColumnSpell={parentColumnUrl}";
                url += $"{parentColumnUrl}";
                currentColumnUrl = parentColumnUrl;
            }
            if (!string.IsNullOrEmpty(childColumnUrl))
            {
                queryString += $"&childColumnspell={childColumnUrl}";
                url += $"/{childColumnUrl}";
                currentColumnUrl = childColumnUrl;
            }
            var data = await _httpHelper.GetAsync<ApiResult<ArticleListPageVM>>(queryString);
            if (data == null)
                return NotFound();
            //分页
            ViewBag.PageHtml = WebHelper.ShowPageNavigate(15, page, (int)data?.Data.Total, $"{_domainConfig.CurrentValue.Host}/{url}/");
            ViewBag.CurrentColumnUrl = currentColumnUrl;
            return View(data?.Data);
        }

        [Route("/about.html")]
        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";
            return View();
        }
        [Route("/link.html")]
        public async Task<IActionResult> Link()
        {
            var data = await _httpHelper.GetAsync<ApiResult<List<CmsAdvListVM>>>($"/api/index/get-adv-list?&siteId={_siteConfig.CurrentValue.GlobalSiteId}&&type={(short)AdvEnum.GoodBlog}");
            ViewBag.BlogRecLinkData = data?.Data;
            return View();
        }
        [HttpGet("/search.html")]
        public async Task<IActionResult> Search(int page = 1, string keyword = null)
        {
            if (string.IsNullOrEmpty(keyword))
            {
                return NotFound();
            }
            string queryString = $"/api/index/get-list?keyword={keyword}&page={page}&siteId={_siteConfig.CurrentValue.GlobalSiteId}";
            var articleList = await _httpHelper.GetAsync<ApiResult<ArticleListPageVM>>(queryString);
            if (articleList == null)
                return NotFound();
            TempData["CurrentKeyword"] = keyword;
            ViewData["Page"] = new StaticPagedList<CmsArtcileVM>(articleList?.Data.ArticleList, page, 15, (int)articleList?.Data.Total);
            return View(articleList.Data);
        }
        [Route("/404.html")]
        public IActionResult Error(int? statusCode = null)
        {
            return View(statusCode);
        }
   
        public async Task<bool> IsExistColumnUrl(params string[] urls)
        {
            if (urls.Length < 0)
            {
                return false;
            }
            var list = await _httpHelper.GetAsync<ApiResult<List<CmsColumnVM>>>($"/api/index/get-all-column?siteId={_siteConfig.CurrentValue.GlobalSiteId}");
            CmsColumnVM model = null;
            foreach (var item in urls)
            {
                if (string.IsNullOrEmpty(item))
                {
                    return false;
                }
                model = list.Data.Where(d => d.EnTitle.Equals(item)).FirstOrDefault();
                if (model == null)
                {
                    return false;
                }
                else
                {
                    continue;
                }
            }
            return true;
        }

    }
}
